using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace rocketjump
{
    public class ParticleEngine
    {
        private Random random;
        public Vector2 EmitterLocation { get; set; }
        private List<Particle> particles;
        public List<Texture2D> textures;
        private int particleTimeToLive;
        public float rotation;
        private float particleSize;
        private int particleAmount;
        Boolean m_started = false;
        private int m_type;

        public ParticleEngine(List<Texture2D> textures, Vector2 location, int particleTimeToLive, float particleSize, int particleAmount, int type)
        {
            EmitterLocation = location;
            this.textures = textures;
            this.particles = new List<Particle>();
            this.particleTimeToLive = particleTimeToLive;
            this.particleSize = particleSize;
            this.particleAmount = particleAmount;
            random = new Random();
            m_type = type;
        }

        private Particle GenerateNewParticle()
        {
            Texture2D texture = textures[random.Next(textures.Count)];
            Vector2 position = EmitterLocation;
            if (m_type == 2)
            {
                position.X = EmitterLocation.X + random.Next(-400, 2400);
            }
            Vector2 velocity = new Vector2(
                    10f * (float)(random.NextDouble() * 2 - 1),
                    10f * (float)(random.NextDouble() * 2 - 1));
            float angle = 0;
            float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
            float size = particleSize;
            int ttl = particleTimeToLive;

            return new Particle(texture, position, velocity, angle, angularVelocity, m_type, size, ttl);
        }

        public void Update()
        {
            if (!m_started)
                return;
            int total = this.particleAmount;

            for (int i = 0; i < total; i++)
            {
                particles.Add(GenerateNewParticle());
            }

            for (int particle = 0; particle < particles.Count; particle++)
            {
                particles[particle].Update();
                if (particles[particle].TTL <= 0)
                {
                    particles.RemoveAt(particle);
                    particle--;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (!m_started)
                return;
            for (int index = 0; index < particles.Count; index++)
            {
                particles[index].Draw(spriteBatch);
            }
        }

        public void Start()
        {
            m_started = true;
        }
        public void Stop()
        {
            m_started = false;
        }
    }
}
